<template>
  <div class="view-box info-box">
    <info-title :title="infoTitle" icon="el-icon-s-custom"></info-title>
    <div class="info-content">
      <div style="width: 50%; margin: 10px auto">
        <el-form ref="form" :model="form" label-width="80px" :rules="rules">
          <el-form-item label="用户名" prop="username">
            <el-input
              class="input-width"
              v-model="form.username"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="性别">
            <el-radio-group v-model="form.gender">
              <el-radio label="男"></el-radio>
              <el-radio label="女"></el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input
              class="input-width"
              v-model="form.email"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input
              class="input-width"
              v-model="form.phone"
              autocomplete="off"
            ></el-input>
          </el-form-item>

          <el-form-item label="角色" prop="role_id">
            <el-select
              class="input-width"
              v-model="form.role_id"
              placeholder="请选择角色"
            >
              <el-option
                v-for="item in roleOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item style="margin-top: 25px">
            <el-button
              type="primary"
              icon="el-icon-check"
              @click="onSubmit('form')"
              >确定</el-button
            >
            <el-button icon="el-icon-back" @click="$router.go(-1)"
              >返回</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
// 导入标题组件
import infoTitle from "../../../../components/common/info-title";
import { addUser, updateUser } from "../../../../api/user";
import { getRoles } from "../../../../api/role";
export default {
  data() {
    return {
      // 标题信息
      infoTitle: "新增角色",
      //   表单信息
      form: {
        username: "",
        gender: "男",
        phone: "",
        email: "",
        role_id: "",
      },
      // 角色信息
      roleOptions: [],
      //   当前是修改还是新增
      type: "add",
      // 表单信息校验
      rules: {
        username: [
          { required: true, message: "用户名称不能为空", trigger: "blur" },
        ],
        // 邮箱校验
        email: [
          {
            pattern: /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/,
            message: "邮箱格式错误",
            trigger: "blur",
          },
        ],
        //手机号校验
        phone: [
          {
            pattern: /^1[3456789]\d{9}$/,
            message: "手机号错误",
            trigger: "blur",
          },
        ],

        role_id: [{ required: true, message: "请选择角色", trigger: "blur" }],
      },
    };
  },
  components: { infoTitle },
  mounted() {
    if (JSON.stringify(this.$route.params) != "{}") {
      this.form = this.$route.params;
      this.infoTitle = "修改角色";
      this.type = "update";
    }
    this.getRolesData();
  },
  methods: {
    // 获取角色信息
    getRolesData() {
      getRoles().then((res) => {
        if (res.success) this.roleOptions = res.data;
      });
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.submitData();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    submitData() {
      if (this.type == "update") {
        updateUser(this.form).then((res) => {
          if (res.success) this.$router.go(-1);
        });
      } else {
        addUser(this.form).then((res) => {
          if (res.success) this.$router.go(-1);
        });
      }
    },
  },
};
</script>

<style>
.view-box {
  padding: 10px;
}
.info-content {
  padding: 10px;
}
</style>